TOP_DIR=../../..

include $(TOP_DIR)/build/build.inc

MY_CTX=payorintel

MY_REL_INSTALL_DIR=$(CUST_REL_INSTALL_DIR)/$(MY_CTX)


# CONFIG DIR
CFG_SRC_FILES= \
  $(wildcard config/*.rc) \

CFG_HSRC_FILES= \


# SCRIPTS DIR
PERL_FILES= \
  $(wildcard scripts/*/*.pl) \


BASH_FILES= \
  $(wildcard scripts/*/*.sh) \
  $(wildcard sql/*/*/*.sh) \


# SQL DIR
SQL_FILES= \
  $(wildcard sql/*/*/*.sql) \


DBDATA_FILES= \
  $(wildcard sql/*/*/dbdata/*.data) \
  $(wildcard sql/*/*/dbdata/*.hdr) \


CFG_REL_INSTALL_FILES=$(CFG_SRC_FILES:%=$(MY_REL_INSTALL_DIR)/%)
CFG_REL_HINSTALL_FILES=$(CFG_HSRC_FILES:%=$(MY_REL_INSTALL_DIR)/.%)

PERL_CHECK=$(PERL_FILES)
PERL_REL_INSTALL_FILES=$(PERL_FILES:%=$(MY_REL_INSTALL_DIR)/%)

BASH_CHECK=$(CFG_SRC_FILES) $(BASH_FILES)
BASH_REL_INSTALL_FILES=$(BASH_FILES:%=$(MY_REL_INSTALL_DIR)/%)

SQL_REL_INSTALL_FILES=$(SQL_FILES:%=$(MY_REL_INSTALL_DIR)/%)
DBDATA_REL_INSTALL_FILES=$(DBDATA_FILES:%=$(MY_REL_INSTALL_DIR)/%)



all: check-bash-syntax check-pl-syntax


install: check-bash-syntax check-pl-syntax
	@@echo "-------: Context files symbolically linked to work area itself"


clean:


install-release: check-bash-syntax check-pl-syntax $(CFG_REL_INSTALL_FILES) $(CFG_REL_HINSTALL_FILES) $(PERL_REL_INSTALL_FILES) $(BASH_REL_INSTALL_FILES) $(SQL_REL_INSTALL_FILES) $(DBDATA_REL_INSTALL_FILES)


$(CFG_REL_INSTALL_FILES): $(CFG_SRC_FILES)
	@for f in $?; do \
           dir=$(MY_REL_INSTALL_DIR)/`dirname $$f`; \
           if [ ! -d $$dir ]; then $(MKDIR) $$dir; $(CHMOD) 700 $$dir; fi; \
           $(CP) $$f $(MY_REL_INSTALL_DIR)/$$f; \
	   $(RDMOD) $(MY_REL_INSTALL_DIR)/$$f; \
           if [ $$? -ne 0 ]; then exit 1; fi; \
        done

# TODO change to handle multple files
$(CFG_REL_HINSTALL_FILES): $(CFG_HSRC_FILES)
	@for f in $?; do \
           dir=$(MY_REL_INSTALL_DIR)/`dirname $$f`; \
           if [ ! -d $$dir ]; then $(MKDIR) $$dir; $(CHMOD) 700 $$dir; fi; \
           if [ $$f -nt $(MY_REL_INSTALL_DIR)/.$$f ]; then \
             $(CP) $$f $(MY_REL_INSTALL_DIR)/.$$f; \
	     $(RDMOD) $(MY_REL_INSTALL_DIR)/$$f; \
             echo -n "" > $(MY_REL_INSTALL_DIR)/.$$f; \
             cat $$f | while read x; do \
               eval "echo $${x}" >> $(MY_REL_INSTALL_DIR)/.$$f; \
             done; \
	     $(RDMOD) $(MY_REL_INSTALL_DIR)/.$$f; \
             if [ $$? -ne 0 ]; then exit 1; fi; \
           fi; \
        done


$(PERL_REL_INSTALL_FILES): $(PERL_FILES)
	@for f in $?; do \
           dir=$(MY_REL_INSTALL_DIR)/`dirname $$f`; \
           if [ ! -d $$dir ]; then $(MKDIR) $$dir; $(CHMOD) 700 $$dir; fi; \
           $(CP) $$f $(MY_REL_INSTALL_DIR)/$$f; \
           $(BINMOD) $(MY_REL_INSTALL_DIR)/$$f; \
           if [ $$? -ne 0 ]; then exit 1; fi; \
        done

$(BASH_REL_INSTALL_FILES): $(BASH_FILES)
	@for f in $?; do \
           dir=$(MY_REL_INSTALL_DIR)/`dirname $$f`; \
           if [ ! -d $$dir ]; then $(MKDIR) $$dir; $(CHMOD) 700 $$dir; fi; \
           $(CP) $$f $(MY_REL_INSTALL_DIR)/$$f; \
           $(BINMOD) $(MY_REL_INSTALL_DIR)/$$f; \
           if [ $$? -ne 0 ]; then exit 1; fi; \
        done


$(SQL_REL_INSTALL_FILES): $(SQL_FILES)
	@for f in $?; do \
           dir=$(MY_REL_INSTALL_DIR)/`dirname $$f`; \
           if [ ! -d $$dir ]; then $(MKDIR) $$dir; $(CHMOD) 700 $$dir; fi; \
           $(CP) $$f $(MY_REL_INSTALL_DIR)/$$f; \
           $(RDMOD) $(MY_REL_INSTALL_DIR)/$$f; \
           if [ $$? -ne 0 ]; then exit 1; fi; \
        done


$(DBDATA_REL_INSTALL_FILES): $(DBDATA_FILES)
	@for f in $?; do \
           dir=$(MY_REL_INSTALL_DIR)/`dirname $$f`; \
           if [ ! -d $$dir ]; then $(MKDIR) $$dir; $(CHMOD) 700 $$dir; fi; \
           $(CP) $$f $(MY_REL_INSTALL_DIR)/$$f; \
           $(RDMOD) $(MY_REL_INSTALL_DIR)/$$f; \
           if [ $$? -ne 0 ]; then exit 1; fi; \
        done


clean-release:
	$(RM) $(CFG_REL_INSTALL_FILES)
	$(RM) $(CFG_REL_HINSTALL_FILES)
	$(RM) $(PERL_REL_INSTALL_FILES)
	$(RM) $(BASH_REL_INSTALL_FILES)
	$(RM) $(SQL_REL_INSTALL_FILES)
	$(RM) $(DBDATA_REL_INSTALL_FILES)


include $(TOP_DIR)/build/rules.mk
